const fs = require("fs")
const sysPath = require("path")
const templates = fs.readdirSync(sysPath.resolve(__dirname, "../../packages"))
  .filter(template => template.indexOf("plugin") > -1)
//移出老版本的目录
let vueTemplates = templates.filter((template) => (template.indexOf("vue") > -1 && template.indexOf("old") == -1))
let reactTemplates = templates.filter((template) => (template.indexOf("vue") == -1 && template.indexOf("svelte") == -1 && template.indexOf("old") == -1))


// updateVueConfig(vueTemplates)
updateReactConfig(reactTemplates)

// updateConfigJsonId([...vueTemplates,...reactTemplates])
function updateReactConfig(templates) {
  templates.forEach((template) => {
    handleReactConfig(`../../packages/${template}`, template)
  })
  handleReactConfig(`../../templates/react`, "")

  function handleReactConfig(basepath, template) {
    let packageJson = require(`${basepath}/package.json`)
    let configJson = require(`${basepath}/pluginTemp/config.json`)
    packageJson["name"] = template
    packageJson.dependencies["antd"] = "4.21.6"
    fs.writeFileSync(
      sysPath.resolve(__dirname, `${basepath}/package.json`),
      JSON.stringify(packageJson, null, 2),
      "utf8"
    )
    // fs.writeFileSync(
    //   sysPath.resolve(__dirname, `${basepath}/pluginTemp/config.json`),
    //   JSON.stringify(configJson, null, 2),
    //   "utf8"
    // )

  }
}


function updateVueConfig(templates) {
  templates.forEach(template => {
    handleVueConfig(`../../packages/${template}`, template)
  })
  handleVueConfig(`../../templates/vue`, "")

  function handleVueConfig(basepath, template) {
    let packageJson = require(`${basepath}/package.json`)
    let configJson = require(`${basepath}/pluginTemp/config.json`)
    packageJson["name"] = template
    packageJson["dependencies"]["querystring"] = "^0.2.1"
    packageJson["devDependencies"]["inquirer"] = "^8.1.2"
    let scripts = packageJson.scripts
    scripts["dev"] = "vue-cli-service serve"
    scripts["predev"] = "node scripts/predev"
    scripts["prebuild"] = "npm run predev && node scripts/prebuild"
    scripts["dev:online"] = "cross-env VUE_APP_ONLINE_DEVELOPMENT_MODE=true vue-cli-service serve"
    scripts["devConf"] = "cross-env VUE_APP_PRODCONF=true vue inspect > output.dev.js"
    delete scripts["serve"]
    delete scripts["preserve"]
    packageJson.scripts = scripts
    configJson["online-development-mode"] = {
      "open": false,
      "host": "http://localhost",
      "port": "8080",
      "entry": "js/app.js"
    }
    fs.writeFileSync(
      sysPath.resolve(__dirname, `${basepath}/package.json`),
      JSON.stringify(packageJson, null, 2),
      "utf8"
    )
    fs.writeFileSync(
      sysPath.resolve(__dirname, `${basepath}/pluginTemp/config.json`),
      JSON.stringify(configJson, null, 2),
      "utf8"
    )
  }
}

function updateConfigJsonId(templates) {
  const generateUUID = () => {
    let d = new Date().getTime()
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
      let r = (d + Math.random() * 16) % 16 | 0
      d = Math.floor(d / 16)
      return (c == "x" ? r : (r & 0x3) | 0x8).toString(16)
    })
  }
  templates.forEach(template => {
    let configJson = require(`../../packages/${template}/pluginTemp/config.json`)
    configJson["id"] = generateUUID()
    fs.writeFileSync(
      sysPath.resolve(__dirname, `../../packages/${template}/pluginTemp/config.json`),
      JSON.stringify(configJson, null, 2),
      "utf8"
    )
  })
}